# -*- coding: utf-8 -*-
from accost.shortcuts import *


class UploadHandler(Handler):
    def get(self):
        from jinja2 import Template
        return Response(Template(u"""
        <form enctype="multipart/form-data" method="post" action=".">
        <input type="file" name="test-file" />
        <input type="submit" value="上传" />
        </form>
        {{ errors.get('test-file', '') }}
        """).render(errors=self.errors))

    def post(self):
        test_file = self.request.files.get('test-file')
        if not test_file:
            self.errors['test-file'] = u'没有文件'
            return self.get()
        f = open('a.iso', 'wb')
        while True:
            s = test_file.read(10 ** 6)
            if s:
                f.write(s)
            else:
                f.close()
                break
        return Response(u'上传完成')
